﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Code;
using DB;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Models;

namespace LayuiAdmin.Controllers {
    [Authorize]
    public class AccountController : Controller {
        [AllowAnonymous]
        public IActionResult Login() {
            return View();
        }

        public IActionResult UserInfo(long id, int userType) {
            //var userTypes = BaseCode.GetEnumByList<UserTypes>(new ItemList() { value = 0, text = "请选择" });


            ViewData["userType"] = userType;

            using (var context = new SqlServerDB()) {
                var model = context.Users.FirstOrDefault(m => m.Id == id);

                if ((UserTypes)userType == UserTypes.普通用户) {
                    var levelCode = BaseCode.GetEnumByList<LevelCode>();

                    ViewData["Level"] = levelCode;

                    CacheHelper cache = new CacheHelper();
                    var area = cache.GetNewAreas();

                    var allPro = area.Where(m => m.parent == 1).ToList();
                    var proList = area.Where(m => m.parent == 1).ToList();

                    allPro.Insert(0, (new Area { id = 0, name = "请选择" }));
                    proList.Insert(0, (new Area { id = 0, name = "请选择" }));

                    ViewData["allPro"] = allPro;

                    var userList = context.Users.Where(m => m.UserType == UserTypes.普通用户);

                    List<Area> cityList = new List<Area>();

                    if (model != null) {
                        userList = userList.Where(m => m.Id != model.Id);

                        if (model.CityId > 0) {
                            var city = area.FirstOrDefault(m => m.id == model.CityId);

                            ViewData["pro"] = city.parent;

                            cityList = area.Where(m => m.parent == city.parent).ToList();

                            foreach (var ul in userList) {
                                if (ul.CityId == 0) continue;
                                var c = cityList.FirstOrDefault(m => m.id == ul.CityId);

                                cityList.Remove(c);
                            }

                            cityList.Insert(0, (new Area { id = 0, name = "请选择" }));

                            ViewData["city"] = cityList;
                        } else {
                            ViewData["pro"] = 0;
                        }
                    }


                    foreach (var ul in userList) {
                        if (ul.ProvinceId == 0) continue;
                        var p = proList.FirstOrDefault(m => m.id == ul.ProvinceId && ul.Id != id);

                        proList.Remove(p);
                    }

                    ViewData["province"] = proList;

                    var u = userList.ToList();

                    u.Insert(0, new Users { Id = 0, UserName = "请选择" });
                    ViewData["users"] = u;
                } else if ((UserTypes)userType == UserTypes.管理员) {
                    if (model != null && model.UserType == UserTypes.超级管理员) return View(model);
                    var roles = context.Roles.ToList();
                    roles.Insert(0, new RoleInfo { Id = 0, Text = "请选择" });

                    ViewBag.Roles = roles;

                    if (model != null) {
                        var u2r = context.UserToRole.FirstOrDefault(m => m.UserId == id);

                        if (u2r != null) {
                            model.ParentId = u2r.RoleId;
                        }
                    }
                }

                return View(model);
            }
        }

        public IActionResult List(int userTypes) {

            ViewData["userTypes"] = userTypes;

            if (HttpContext.User.Identity.GetValue("UserType") == ((int)UserTypes.普通用户).ToString()) {
                ViewBag.IsShowOverage = 0;
            } else if (userTypes == (int)UserTypes.普通用户) {
                ViewBag.IsShowOverage = 99;
            }

            var list = BaseCode.GetEnumByList<LevelCode>(new ItemList() { value = 0, text = "请选择" });

            ViewData["title"] = userTypes == (int)UserTypes.普通用户 ? "推荐机构" : "后台会员管理";

            return View(list);
        }

        public IActionResult RecommendList() {
            var list = BaseCode.GetEnumByList<LevelCode>(new ItemList() { value = 0, text = "请选择" });

            ViewData["Id"] = HttpContext.User.Identity.GetValue(ClaimTypes.Sid);
            ViewBag.UserType = (UserTypes)Convert.ToInt32(HttpContext.User.Identity.GetValue("UserType"));

            return View(list);
        }

        public IActionResult Recommend(long id) {
            using (var context = new SqlServerDB()) {
                var model = context.Users.FirstOrDefault(m => m.Id == id);

                if (model != null) {
                    return View(model);
                }
            }
            return View();
        }

        public IActionResult ChangePassword() {
            return View();
        }

        public IActionResult SecondaryPwd(long Id) {
            using (var context = new SqlServerDB()) {
                var user = context.Users.FirstOrDefault(m => m.Id == Id);

                return View(user);
            }
        }

        public IActionResult Roles() {
            CacheHelper cache = new CacheHelper();
            List<Area> list = new List<Area>();
            var menus = cache.GetMenuList();

            var parent = menus.Where(m => m.parent == 0);

            foreach (var m in parent) {
                var children = menus.Where(w => w.parent == m.id);

                if (children != null && children.Count() > 0) {
                    list.AddRange(children);
                } else {
                    list.Add(m);
                }
            }

            return View(list);
        }

        public IActionResult Detail(long Id) {
            using (var context = new SqlServerDB()) {
                var cashList = BaseCode.GetEnumByList<CashType>();
                cashList = cashList.FindAll(m => m.value == 1);
                ViewData["cash"] = cashList;

                CacheHelper cache = new CacheHelper();
                var area = cache.GetAreaList();

                var allPro = area.Where(m => m.parent == 1).ToList();
                allPro.Insert(0, (new Area { id = 0, name = "请选择" }));
                ViewData["pro"] = allPro;

                var user = context.Users.FirstOrDefault(m => m.Id == Id);

                context.Entry(user).Collection(u => u.Cash).Query().FirstOrDefault();

                context.Entry(user).Collection(u => u.ShippingAddress).Query().ToList();

                if (user.ParentId > 0) {
                    var parentUser = context.Users.FirstOrDefault(m => m.Id == user.ParentId);

                    ViewData["Parent"] = parentUser.UserCode;
                }

                var setting = context.Settings.Where(m => m.SystemType == SystemType.提现);
                var time = setting.FirstOrDefault(m => m.OtherType == ((int)SystemType.提现日期).ToString());
                var multiple = setting.FirstOrDefault(m => m.OtherType == ((int)SystemType.提现倍数).ToString());

                string tips = "";

                if (time != null) {
                    var days = time.Values.Split(',');

                    foreach (var d in days) {
                        if (d == DateTime.Now.Day.ToString()) {
                            ViewData["show"] = true;
                            break;
                        }
                    }

                    tips = $"每月 {time.Values.Replace(",", "，")} 号才能申请";
                }

                if (multiple != null && (multiple.Values + "").Length > 0 && !multiple.Values.Equals("0")) {
                    tips += $"且提现金额必须为 {multiple.Values} 倍数";
                }

                ViewData["tips"] = tips;

                if (user.ShippingAddress.Count > 0) {
                    var address = user.ShippingAddress.FirstOrDefault();

                    if (address != null) {
                        if (address.CityId > 0) {
                            var city = area.Where(m => m.parent == address.ProvinceId).ToList();
                            city.Insert(0, (new Area { id = 0, name = "请选择" }));
                            ViewData["city"] = city;
                        }

                        if (address.AreaId > 0) {
                            var alist = area.Where(m => m.parent == address.CityId).ToList();
                            alist.Insert(0, (new Area { id = 0, name = "请选择" }));
                            ViewData["area"] = alist;
                        }
                    }
                }

                return View(user);
            }
        }
    }
}